package org.brisling.duties.queryService;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpSession;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.exception.DangerousOperatorException;
import org.brisling.common.util.WhereUtil;
import org.brisling.duties.domain.DutiesDomain;
import org.brisling.sysman.queryService.QueryDictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@SuppressWarnings("rawtypes")
@RestController
@RequestMapping("/workduties/query")
public class QueryDutieService extends AbstractBaseQueryService<DutiesDomain>{
	
	
	
	@Autowired
	private QueryDictionaryService dicService;
	
	public QueryDutieService(){
		this.moduleName = "workduties";
		this.subSystem = ConstSysParam.SUBSYSTEM_DUTIES;
		this.tableName = ConstSysParam.VIEW_PREFIX +"duties";
		this.clazz = DutiesDomain.class;
		
		
		
		
		orderMap.put("over_days","desc");
			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/duties/workduties/main");
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/duties/workduties/editDlg");
	}
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<DutiesDomain> getListDomain(
			@RequestBody(required=false) String body,HttpSession session){
		
		String userid = getUserid(session);
		Map<String,Object> conditionMap =this.getBodyParam(body); 
		if(!this.isSuperDept(userid,"query_all", "query_duties_all"))		
			conditionMap=this.addDeptConditionToMap(userid,conditionMap, "doct_depart_code");
		
		return super.getListDomainByConditionAndOrder(null,conditionMap, this.getTypeMap(DutiesDomain.class), orderMap,true);
		
		
	}
	
	/**
	 * 获取统计图表信息
	 * @param body	前端参数
	 * @return List
	 */
	@SuppressWarnings({"unchecked" })
	@RequestMapping("/getStaticInfo.action")
	public List<LinkedHashMap> getStaticInfo(@RequestBody(required=false) String body){
		String sql = "select sxzt as \"name\",count(1)as \"y\" from  " + this.getTableName();
		Map<String,Object> map =getBodyParam(body);			
		
		String whereCorse = WhereUtil.getConditionWhereCorse((Map<String, String>) map.get("condition"),this.getTypeMap(DutiesDomain.class));
		
		if((whereCorse!=null)&&(whereCorse.trim().length()>0)){
			sql +=" where " + whereCorse;
		}
		sql += " group by sxzt";
		return super.getMappedList(sql);
	}
	
	/**
	 * 获取统计图表信息
	 * @param body	前端参数
	 * @return List
	 */
	@SuppressWarnings({ "unchecked" })
	@RequestMapping("/getOverdaysInfo.action")
	public List<LinkedHashMap> getOverdaysInfo(@RequestBody(required=false) String body){
		String sql = "select sxzt as \"name\",count(1)as \"y\" from  " + this.getTableName();
		Map<String,Object> map =getBodyParam(body);			
		
		String whereCorse = WhereUtil.getConditionWhereCorse((Map<String, String>) map.get("condition"),this.getTypeMap(DutiesDomain.class));
		
		if((whereCorse!=null)&&(whereCorse.trim().length()>0)){
			sql +=" where " + whereCorse;
		}
		sql += " group by sxzt";
		return super.getMappedList(sql);
	}
	
	/**
	 * 获取实体列表对象
	 * @param page
	 * @param rows
	 * @param deptid
	 * @param sort
	 * @return
	 */
	
	@RequestMapping("/getList.action")
	public List<DutiesDomain> getList(@RequestParam(value="page",required=false) Integer page,
			@RequestParam(value="rows",required=false) Integer rows,
			@RequestParam(value="deptid",required=false) Integer deptid,
			@RequestParam(value="sort",required=false) String sort){		
		
		StringBuilder sb = new StringBuilder();
		String sql = "";
		if(deptid!=null){
			String whereCorse=String.format(sb.append("\"deptid\" = %1$d").toString(),deptid);
			sql = String.format(this.TW_SQL, this.tableName,whereCorse);
		}else{
			sql = String.format(this.T_SQL, this.tableName);
		}
		
		return super.getPagedList(page,rows,sql);
	}
	
	/**
	 * 查询职能监控异常数据
	 * @return null
	 */
	public List<DutiesDomain> getWindowAntiDose(){				
		String sql =dicService.getSqlByBizcode("medical_window_dose");		
		
		try {
			return super.getClassedAllList(sql, DutiesDomain.class);
		} catch (DangerousOperatorException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 	
		return null;
	}	
	
	
	
}
